using System;
using System.Collections.Generic;
using System.Net;
using SpoonCore2.Message;
using SpoonCore2.Network;
using SpoonCore2.OverlayNetwork;
using SpoonCore2.Service;
using SpoonCore2.OverlayNetwork.Pastry;
using SpoonCore2.Client;
using SpoonCore2.Service.Broadcast;
using SpoonCore2.Service.Lookup;

namespace SpoonCore2
{
	public class SpoonClient : IDisposable
	{
		private readonly ISpoonSocket _socket;
		private readonly IOverlayNetwork _network;
		private readonly ServiceManager _manager;

		public SpoonClient()
			: this(0)
		{ }

		public SpoonClient(int port)
		{
			_socket = new SpoonUDPSocket(port);
			_network = new PastryNetwork();
			_manager = new ServiceManager(_socket, _network);

			Init();
		}

		private void Init()
		{
			_manager.LoadAll();

			//BroadcastTest test = new BroadcastTest(_manager); //TODO just for test
		}

		public IOverlayNetwork Network
		{
			get { return _network; }
		}

		public void Dispose()
		{
			_socket.Dispose();
			_network.Dispose();
		}

		public void Start()
		{
			_network.Start();
		}
	}
}